/**
 * Created by dmrj on 2016/5/3.
 */
define(['jquery', 'appLocalStorage', 'appConst','model/global', 'appMessageBox'], function ($, appLocalStorage, appConst,global, msgBox) {

    var that;
    var component = {};
    var data= {
        searchFields: {},
        searchKey_plist_code: "ispc.search.project.code." + global.getUserId()
    };

    return {
        init: function () {
            that = this;
            data.searchFields = {};
            component = {
              "tbl_plist": $("#dt_projects"),
              "btn_plist_search": $("#btn-simple-search"),
              "input_plist_code": $("#search-input"),
              "input_plist_name": $("#search-input1"),
            };

            that.initPage();
            that.initTable();
            that.initEvent();
        },

        initTable: function () {

            var customParams = function (params) {
                $.extend(params, data.searchFields);
                params['draw'] = params.draw;
                return params;
            };

            //console.log(data.searchFields);

            component.tbl_plist.dataTable({
                "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'><'col-sm-6 col-xs-12 hidden-xs'>>r" +
                "t" +
                "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
                "pageLength": global.getPageSize(),
                "paging": true,
                "processing":true,
                "autoWidth": false,
                "serverSide": true,
                "language": appConst.table_language(),
                "ordering": false,
                "ajax": {
                    "url": 'project/list',
                    "type": "POST",
                    "dataSrc": "body",
                    "traditional": true,
                    "data": customParams,
                    "error": function (e) {
                        if (e.status == 401) {
                            msgBox.sessionFailureBox();
                        }
                        if (e.status == 500) {
                            msgBox.serverFailureBox();
                        }
                        if (e.status == 404) {
                            msgBox.pathFailureBox();
                        }
                    }
                },
                "columns": [
                    {"data": null},
                    {"data": "id"},
                    {"data": "code"},
                    {"data": "name"},
                    {"data": "description"},
                    {"data": null}
                ],
                "columnDefs": [
                    {
                        "targets": [1],
                        "visible": false
                    }, {
                        "targets": [4],
                        "render": function (data, type, full, meta) {
                            var str = '<span data-toggle="tooltip" title="' + data + '">' + data.substr(0, 38) + '……</span>';
                            var description = data.length > 40 ? str : data;
                            return description;
                        }
                    },
                    {
                        "targets": [5],
                        "render": function (data, type, full) {
                            var operate = '<div class="center">'
                                + '<a href="#project/update/' + full.id +'" >' + $.i18n.prop('PROJECT_OPERATE_DETAIL') + '</a> '
                                + '</div>';

                            return operate;
                        }
                    }
                ]
            });
            $("[rel=tooltip]").tooltip();
            component.tbl_plist.DataTable().on('draw.dt', function () {
                var dt = $(this).dataTable();
                dt.api().column(0, {draw: 'applied'}).nodes().each(function (cell, i) {
                    var pageNum = dt.api().page();
                    var pageSize = dt.api().page.len();
                    cell.innerHTML = pageNum * pageSize + i + 1;
                });
            });
        },

        initEvent: function () {
            component.btn_plist_search.click(function (e) {

                var obj = {};
                var fieldName = component.input_plist_code.attr("name");
                var fieldName1 = component.input_plist_name.attr("name");

                obj[fieldName1] = component.input_plist_name.val();
                obj[fieldName] = component.input_plist_code.val();
                data.searchFields = obj;
                component.tbl_plist.DataTable().ajax.url('project/list').load();
            });

            component.input_plist_code.on("keyup", function (event) {
                var e = event || window.event || arguments.callee.caller.arguments[0];
                if (e && e.keyCode == 13) {
                    component.btn_plist_search.trigger("click");
                }
            });

            component.input_plist_name.on("keyup", function (event) {
                var e = event || window.event || arguments.callee.caller.arguments[0];
                if (e && e.keyCode == 13) {
                    component.btn_plist_search.trigger("click");
                }
            });
        },

        initPage: function () {
            var queryCode = appLocalStorage.getLocal(data.searchKey_plist_code);
            var obj = {};
            if (queryCode != null && queryCode != "") {
                component.input_plist_code.val(queryCode);
                var fieldName = component.input_plist_code.attr("name");
                obj[fieldName] = component.input_plist_code.val();
                data.searchFields = obj;
            }
        }
    }
});